%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 24. 12月 2019 17:51
%%%-------------------------------------------------------------------
-module(client).
-author("Administrator").
-include("include/player.hrl").
-include("include/game.hrl").
-include("include/myshare.hrl").
-include("include/logger.hrl").
-include("include/uri.hrl").

%% API
-export([load_player/0, start_task/0, add_task/1]).

%%模拟客服端加载玩家
load_player() ->
  PlayerList = database:select_player_info(),
  Fun = fun(Player) ->
    player_assist:login(Player)
        end,
  lists:foreach(Fun, PlayerList).


start_task() ->
  Rows = database:select_task(),
  ?DEBUG("task data :~p", [Rows]),
  task_data(Rows).

%%任务数据
task_data(Data) ->
  Fun2 = fun(Row) ->
    add_task(Row)
         end,
  lists:foreach(Fun2, Data).

%%将任务数据添加到任务表
add_task(Row) ->
  [Id, Name, GameId, Player_number, Condition, Take_number, All_day_run, Look_number, {_, Start_at}, {_, End_at}, Start_number, End_number] = Row,
  ?DEBUG("task info row:~p;game_id:~p", [Row, GameId]),
  GameList = sys_util:json_to_list(GameId),
  Start_s = sys_util:datetime_to_timestamp(sys_util:time_to_date_time(Start_at)),
  End_s = sys_util:datetime_to_timestamp(sys_util:time_to_date_time(End_at)),
  if
    Start_s > End_s ->
      End_s2 = End_s + ?DAY_TIMESTAMP;
    true ->
      End_s2 = End_s
  end,
  Is_all_day_run =
    case All_day_run of
      1 -> true;
      _ -> false
    end,
  GameTask =
    #game_task{
      task_id = Id,
      name = binary_to_list(Name),
      start_at = Start_s,
      end_at = End_s2,
      condition = Condition,
      gameId_list = GameList,
      take_start_num = Start_number,
      take_end_num = End_number,
      take_number = Take_number,
      is_all_day_run = Is_all_day_run,
      look_number = Look_number,
      player_number = Player_number
    },
  ets:insert(?ETS_GAME_TASK, GameTask).









